<template>
  <div class="mb-20px">
    <el-button
      @click="
        () => {
          $router.back()
          delVisitedView(route)
        }
      "
      >返回</el-button
    >
  </div>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="auto"
    >
      <el-form-item label="报名ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入报名ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付金额" prop="schemePrice">
        <div class="flex items-center">
          <el-input
            v-model="queryParams.schemePrice[0]"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
            class="!w-78px"
            type="number"
            min="0"
          />
          <span class="m-x-10px">-</span>
          <el-input
            v-model="queryParams.schemePrice[1]"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
            class="!w-78px"
            type="number"
            min="0"
          />
        </div>
      </el-form-item>
      <el-form-item label="会议状态">
        <el-select
          v-model="queryParams.meetingStatus"
          placeholder="状态"
          clearable
          class="!w-240px"
        >
          <el-option label="未开始" value="未开始" />
          <el-option label="进行中" value="进行中" />
          <el-option label="已结束" value="已结束" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="参会方案" prop="meetingPlanId">
        <el-select
          v-model="queryParams.meetingPlanId"
          placeholder="请选择参会方案"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option v-for="i in meetingPlanList" :key="i.id" :label="i.schemeName" :value="i.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="会议时间" prop="meetingTime">
        <el-date-picker
          v-model="queryParams.meetingTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="会议地址" prop="meetingAddress">
        <el-input
          v-model="queryParams.meetingAddress"
          placeholder="请输入会议地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="isFree">
        <el-select
          v-model="queryParams.isFree"
          placeholder="请选择"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option label="待审核" :value="0" />
          <el-option label="已通过" :value="1" />
          <el-option label="已驳回" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="会议ID" prop="meetingId">
        <el-input
          v-model="queryParams.meetingId"
          placeholder="请输入会议ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary"
          ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
        >
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['biz:conference-registration-information:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <!-- <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['biz:conference-registration-information:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column label="报名ID" align="center" prop="id" />
      <el-table-column label="会议ID" align="center" prop="meetingId" />
      <el-table-column label="会议名称" align="center" prop="meetingName" />
      <el-table-column label="会议时间" align="center" prop="startTime">
        <template #default="{ row }">
          {{ formatDate(row.startTime) }} - {{ formatDate(row.endTime) }}
        </template>
      </el-table-column>
      <el-table-column label="会议地址" align="center" prop="meetingAddress" />
      <el-table-column label="会议状态" align="center" prop="meetingStatus" />
      <el-table-column label="参会方案" align="center" prop="schemeName" />
      <el-table-column label="支付金额（元）" align="center" prop="schemePrice" />
      <el-table-column label="状态" align="center" prop="isFree">
        <template #default="{ row }">
          {{ row.isFree === 0 ? '待审核' : row.isFree === 1 ? '已通过' : '已驳回' }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import {
  ConferenceRegistrationInformationApi,
  ConferenceRegistrationInformation
} from '@/api/biz/conferenceregistrationinformation'
import { MeetingPlanApi, MeetingPlan } from '@/api/biz/meetingplan'
import { formatDate } from '@/utils/formatTime'
import { useTagsViewStore } from '@/store/modules/tagsView'

const { delVisitedView } = useTagsViewStore()
const route = useRoute()

/** 会议报名信息 列表 */
defineOptions({ name: 'ConferenceRegistrationInformationUser' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const userId = useRoute().params.id

const loading = ref(true) // 列表的加载中
const list = ref<ConferenceRegistrationInformation[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  nickname: undefined,
  mobile: undefined,
  meetingStatus: undefined,
  meetingTime: [],
  meetingId: undefined,
  userId,
  meetingPlanId: undefined,
  isFree: undefined,
  createTime: [],
  schemePrice: [],
  meetingAddress: undefined,
  isFreeMeeting: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const meetingPlanList = ref<MeetingPlan[]>([]) // 会议计划列表

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data =
      await ConferenceRegistrationInformationApi.getConferenceRegistrationInformationPage(
        queryParams
      )
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

// 获取会议计划列表
const getMeetingPlanList = async () => {
  const data = await MeetingPlanApi.getMeetingPlanPage({
    pageNo: 1,
    pageSize: 100
  })
  meetingPlanList.value = data.list
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ConferenceRegistrationInformationApi.deleteConferenceRegistrationInformation(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除会议报名信息 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await ConferenceRegistrationInformationApi.deleteConferenceRegistrationInformationList(
      checkedIds.value
    )
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: ConferenceRegistrationInformation[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data =
      await ConferenceRegistrationInformationApi.exportConferenceRegistrationInformation(
        queryParams
      )
    download.excel(data, '会议报名信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onActivated(() => {
  getList()
  getMeetingPlanList()
})
</script>
